package com.etc.demo.utils;
import jdk.nashorn.internal.scripts.JD;

import java.net.URI;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBC {
    private JDBC(){}

    //链接对象
    private static Connection con = null;

    private static final  String URL = "jdbc:mysql://localhost:3306/xsgl";
    private static final  String username = "root";
    private static final  String password = "12345678";


    /**
     * 静态代码快 jdbc工具类 单列模式
     * @return
     */

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }


    /**
     * 获取链接对象
     * @return
     * @throws SQLException
     */
    public static Connection getCon() throws SQLException {
        if (con == null){
            con = DriverManager.getConnection(URL, username, password);
        }
        return con;
    }


    /**
     * 关闭资源
     * @param con 链接对象
     */
    public void closeConnection(Connection con) throws SQLException {
        con.close();
    }
}
